package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/mattn/go-sqlite3"
	"myBlog/utils"
	"os"
	"path"
	"strconv"
)

func getAllNotes(c *gin.Context) {
	stmt, _ := db.Prepare("SELECT * FROM note")
	rows, err := stmt.Query()
	if err != nil {
		c.Error(err)
	}
	ans := make([]note, 0)
	for rows.Next() {
		var tem note
		rows.Scan(&tem.Id, &tem.Name, &tem.Time, &tem.Views)
		ans = append(ans, tem)
	}
	c.JSON(200, gin.H{
		"list": ans,
	})
}

//getFileInfo 根绝path+name 获取文章信息
func fetchNoteInfo(c *gin.Context) {
	noteName := c.Query("name")
	var id int64
	var name string
	var views int
	var time int64
	//	从数据库中读取
	stmt, err := db.Prepare("SELECT * from note where `name` = ?")
	//	读取失败--插入初始化数据
	rows, err := stmt.Query(noteName)
	if err != nil {
		c.Error(err)
	}
	if rows.Next() {
		err = rows.Scan(&id, &name, &time, &views)
		if err != nil {
			c.Error(err)
		}
		//注意如果不读取完所有的rows的话--会报database is locked的错误！！！
		for rows.Next() {
		}
		pwd, _ := os.Getwd()
		nPath := path.Join(pwd, "article", noteName)
		time, _ := utils.GetUpdateTime(utils.DealPath(nPath))
		stmt, err = db.Prepare("UPDATE note set `views` = ? , `time` = ? where `name` = ?")
		if err != nil {
			c.Error(err)
		}
		_, err = stmt.Exec(views+1, time, noteName)
		if err != nil {
			c.Error(err)
		}
		c.JSON(200, gin.H{
			"id":    id,
			"views": views + 1,
			"time":  time,
		})
	} else {
		stmt, err := db.Prepare("INSERT INTO note (`name`,`time`,`views`) values (?,?,?)")
		pwd, _ := os.Getwd()
		nPath := path.Join(pwd, "article", noteName)
		time, _ := utils.GetUpdateTime(utils.DealPath(nPath))
		if err != nil {
			c.Error(err)
		}
		result, err := stmt.Exec(noteName, time, 1)
		id, _ = result.LastInsertId()
		c.JSON(200, gin.H{
			"id":    id,
			"views": 1,
			"time":  time,
		})
	}
}

func getAllNoteComments(c *gin.Context) {
	stmt, err := db.Prepare("select * from noteComment")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query()
	if err != nil {
		c.Error(err)
	}
	comments := make([]noteComment, 0)
	for rows.Next() {
		var com noteComment
		rows.Scan(&com.Id, &com.NoteId, &com.Name, &com.Email, &com.Time, &com.Content, &com.Website, &com.Pname, &com.Pcontent)
		comments = append(comments, com)
	}
	fmt.Println(comments)
	c.JSON(200, gin.H{
		"list": comments,
	})
}

// fetchComments 或许文章id对应的评论
func fetchComments(c *gin.Context) {
	pid := c.Query("id")
	t, err := strconv.Atoi(pid)
	if err != nil {
		c.Error(err)
	}
	id := int64(t)
	stmt, err := db.Prepare("select * from noteComment where `noteId` = ?")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query(id)
	if err != nil {
		c.Error(err)
	}
	comments := make([]noteComment, 0)
	for rows.Next() {
		var com noteComment
		//一定要全部读出来！！！这里id不读其他数据也读不出来
		rows.Scan(&com.Id, &com.NoteId, &com.Name, &com.Email, &com.Time, &com.Content, &com.Website, &com.Pname, &com.Pcontent)
		comments = append(comments, com)
	}
	fmt.Println(comments)
	c.JSON(200, gin.H{
		"list": comments,
	})
}

// sendComment 想数据库中插入一条评论
func sendComment(c *gin.Context) {
	var com noteComment
	c.ShouldBindJSON(&com)

	stmt, err := db.Prepare("INSERT INTO noteComment (`noteId`,`name`,`email`,`time`,`content`,`website`,`pname`,`pcontent`)" +
		"values (?,?,?,?,?,?,?,?)")
	if err != nil {
		c.Error(err)
	}
	println(com.NoteId, com.Content)
	result, err := stmt.Exec(com.NoteId, com.Name, com.Email, com.Time, com.Content, com.Website, com.Pname, com.Pcontent)
	if err != nil {
		c.Error(err)
	}
	Nid, err := result.LastInsertId()
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{
		"id": Nid,
	})
}

func delNoteById(c *gin.Context) {
	id := c.Query("id")
	stmt, err := db.Prepare("DELETE FROM note WHERE `id` = ?")
	if err != nil {
		c.Error(err)
	}
	_, err = stmt.Exec(id)
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{})
}

func delNoteCommentById(c *gin.Context) {
	id := c.Query("id")
	stmt, err := db.Prepare("DELETE FROM movieNoteComment WHERE `id` = ?")
	if err != nil {
		c.Error(err)
	}
	_, err = stmt.Exec(id)
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{})
}

func InitArticle(r *gin.Engine) {
	g := r.Group("/article")
	g.POST("/fetchNoteInfo", fetchNoteInfo)
	g.POST("/sendComment", sendComment)
	g.POST("/fetchComments", fetchComments)
}
